<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 陈风任 <491085389@qq.com>
 * Date: 2019-06-21
 */

namespace app\admin\controller;

use think\Page;
use think\Db;

class UsersMoneyRechargeType extends Base {

    /**
     * 构造方法
     */
    public function __construct(){
        parent::__construct();
        $this->recharge_type_db = Db::name('users_money_recharge_type');
    }

    // 级别列表
    public function index()
    {
        $list = array();
        $keywords = input('keywords/s');
        $status = input('status/d');

        $condition = array();

        if (!empty($keywords)) {
            $condition['title'] = array('LIKE', "%{$keywords}%");
        }
        if (!empty($status)) {
            $condition['status'] = $status;
        }

        $count = $this->recharge_type_db->where($condition)->count();
        $Page = new Page($count, config('paginate.list_rows'));
        $list = $this->recharge_type_db
            ->where($condition)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();

        $show = $Page->show();
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->assign('pager',$Page);

        return $this->fetch();
    }

    // 新增
    public function add()
    {
        if (IS_POST) {
            $post = input('post.');
            $post['title'] = trim($post['title']);

            if (empty($post['title'])) $this->error('活动名称不可为空！');
            if (empty($post['money'])) $this->error('奖励条件值不可为空！');

            $post['add_time'] = getTime();
            $post['update_time'] = getTime();

            $ReturnId = $this->recharge_type_db->insert($post);
            if ($ReturnId) {
                \think\Cache::clear('users_money_recharge_type');
                adminLog("新增充值活动 ：{$post['title']}");
                $this->success('操作成功', url('UsersMoneyRechargeType/index'));
            } else {
                $this->error('操作失败');
            }
        }

        return $this->fetch();
    }

    // 编辑
    public function edit()
    {
        if (IS_POST) {
            $post = input('post.');
            $post['title'] = trim($post['title']);

            if (empty($post['title'])) $this->error('活动名称不可为空！');
            if (empty($post['money'])) $this->error('奖励条件值不可为空！');

            $post['update_time'] = getTime();
            $res = $this->recharge_type_db->where('id',$post['id'])->update($post);
            if ($res !== false) {
                \think\Cache::clear('users_money_recharge_type');
                adminLog("编辑充值活动：{$post['title']}");
                $this->success('操作成功', url('UsersMoneyRechargeType/index'));
            } else {
                $this->error('操作失败');
            }
        }
        $id = input('param.id/d');
        $info = $this->recharge_type_db->where('id',$id)->find();
        if (empty($info)){
            $this->error('充值活动不存在');
        }
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function del()
    {
        $id = input('del_id/a');
        $id = eyIntval($id);
        if (IS_AJAX_POST && !empty($id)) {
            // 删除统一条件
            $Where = [
                'id' => ['IN', $id],
            ];
            $title = $this->recharge_type_db->where($Where)->column('title');

            $return = $this->recharge_type_db->where($Where)->delete();
            if ($return) {
                \think\Cache::clear('users_money_recharge_type');
                adminLog('删除充值活动：' . implode(',', $title));
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }
        $this->error('参数有误');
    }
}